package _1_PIO.excel.样式;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 自定义单元格背景颜色
 */
public class _6_CustomColors {

    @Test
    public void _2003(){}

    @Test
    public void _2007() throws IOException {

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet xssfSheet = xssfWorkbook.createSheet();
        XSSFRow xssfRow = xssfSheet.createRow(1);
        XSSFCell xssfCell = xssfRow.createCell(1);

        xssfCell.setCellValue("custom XSSF colors");

        XSSFCellStyle xssfCellStyle = xssfWorkbook.createCellStyle();
        xssfCellStyle.setFillForegroundColor(new XSSFColor( new java.awt.Color(221, 217, 196), new DefaultIndexedColorMap()));
        xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        xssfCell.setCellStyle(xssfCellStyle);

        try(OutputStream outputStream = new FileOutputStream("D:\\workbook.xlsx")){
            xssfWorkbook.write(outputStream);
        }
        finally{
            xssfWorkbook.close();
        }
    }

    @Test
    public void _2007_2() throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);

        cell.setCellValue("custom XSSF colors");

        XSSFCellStyle xssfCellStyle = (XSSFCellStyle) workbook.createCellStyle();
        xssfCellStyle.setFillForegroundColor(new XSSFColor( new java.awt.Color(221, 217, 196), new DefaultIndexedColorMap()));
        xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        cell.setCellStyle(xssfCellStyle);

        try(OutputStream outputStream = new FileOutputStream("D:\\workbook.xlsx")){
            workbook.write(outputStream);
        }
        finally{
            workbook.close();
        }
    }
}
